from fastapi import APIRouter,File,UploadFile
from typing import List
import os

upload = APIRouter()

# 不考慮，文件大小直接進入內存，容易崩壞
@upload.post("/upload")
async def uploadFile(file:bytes = File()):
    return {"fileSize":len(file)}

# 文件句柄
@upload.post("/uploadFile")
async def uploadFile(file:UploadFile):
    path = os.path.join("apps","api","imgs",file.filename)
    with open(path,"wb") as f:
        for chunk in file.file:
            f.write(chunk)
    return {"fileSize": file.size, "filename":file.filename}

@upload.post("/uploadFiles")
async def uploadFile2(file:List[UploadFile]):
    return {"fileSize": [file.size for file in file], "filename":[file.filename for file in file]}